package com.fang.security.security.handler;

import com.alibaba.fastjson.JSON;
import com.fang.security.common.CommonConstants;
import com.fang.security.common.base.ServiceCode;
import com.fang.security.common.result.JsonResult;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Description 权限拒绝处理逻辑
 * @Author Bernie
 * @Date 2024/4/23 10:45
 **/
@Component
public class CustomizeAccessDeniedHandler implements AccessDeniedHandler {
    /**
     * 暂无权限返回结果
     * @Author Sans
     * @CreateTime 2019/10/3 8:41
     */
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException exception) throws IOException {
        JsonResult result = JsonResult.fail(ServiceCode.INSUFFICIENT_PERMISSIONS);
        response.setContentType(CommonConstants.CommonResponse.JSON_U8_CONTENT_TYPE);
        response.getWriter().write(JSON.toJSONString(result));
    }
}
